package com.cyj.redissign.cyj.service.impl;


import com.cyj.redissign.cyj.service.SignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Transactional
@Service
public class SignServiceImpl implements SignService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String prefix = "sign_*";

    @Override
    public Map doSign(String name) {
        Map result = new HashMap();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = df.format(new Date());
        String redisKey = "sign_"+name+"_"+date;
        if (redisTemplate.hasKey(redisKey)){
            result.put("status",1);
            result.put("msg","你已完成签到");
            return result;
        }
        redisTemplate.opsForValue().set(redisKey,"100",60*20,TimeUnit.SECONDS);
        result.put("status",2);
        result.put("msg","签到成功");
        return result;
    }

    @Override
    public Map signList() {
        Map result = new HashMap();
        Set keys = redisTemplate.keys(prefix);
        result.put("data",keys);
        return result;
    }
}
